<?php
namespace app\api\controller\wsgoods;

class Goodscat extends Base {

    public function lists(){
        $d = $this->post();
        $rule = [
            'parent_id'=>'require|integer',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);
        $map = [];
        $map[] =["and","status","=",0];
        if ($d['parent_id']!='') $map[] =["and","parent_id","=",$d['parent_id']];
        $w = where($map);
        $field = ['id', 'cat_title', 'parent_id', 'sub_title', 'icon','pic'];
        $field = implode(",", $field);
        $cat = $this->db->query("select {$field} from goods_cat {$w[0]} order by sort desc,id desc",$w[1]);
        suc($cat);
    }
    /**
     *  获取商品分类树
     * @return [type] [description]
     */
    public function getcattree(){
        #1级分类
        $cat_list = $this->db->query('select * from goods_cat where status = 0 order by sort desc,id desc',[]);
        $new_list = [];
        foreach ($cat_list as $k => $v) {
            if($v['parent_id'] != 0){
                continue;
            }
            if(isset($pid) && $v['id'] != $pid && $pid != 0){
                continue;
            }
            $child_cat = [];
            foreach ($cat_list as $ke => $va) {
                if($v["id"] == $va['parent_id']){
                    foreach ($cat_list as $key => $val) {
                        if($va["id"] == $val['parent_id']){
                            $va["child_list"][] = $val;
                        }
                    }
                    if(!isset($va['child_list'])) $va['child_list'] = [];
                    $child_cat[] = $va;
                }
            }
            $v["child_list"] = $child_cat;
            $new_list[] = $v;
        }
        suc($new_list);
    }

    /**
     * 获取
     */
    public function getChildCat(){
        $d = $this->post();
        $cat_list = $this->db->query('select * from goods_cat where status = 0 and parent_id=? order by sort desc,id desc',[$d['id']]);
        foreach ($cat_list as $k => $v) {
            $child_list = $this->db->query('select * from goods_cat where status = 0 and parent_id=? order by sort desc,id desc',[$v['id']]);
            $cat_list[$k]['child_list'] = $child_list;
        }
        suc($cat_list);
    }
}
?>
